import random

shop = [["水", 1], ["辣条", 2], ["瓜子", 5], ["饼干", 8], ["肉", 10], ["电脑", 100]]
car = []
# 初始化金额
while True:
    money = input("请输入您的钱包余额")
    if money.isdigit():
        money = int(money)
        break
    else:
        print("输入错误，请正确输入金额")

# 优惠券
con = []
for i in range(0, 21):
    con.append('肉')
for i in range(0, 11):
    con.append('电脑')
random_num = random.randint(0, 29)
if con[random_num] == '肉':
    print("免费赠送优惠券一张：肉类3折")
else:
    print("免费赠送优惠券一张：电脑8折")

# 展示商品信息
if con[random_num] == '肉':
    print("商品信息")
    shop_con=[["水", 1], ["辣条", 2], ["瓜子", 5], ["饼干", 8], ["肉", 3], ["电脑", 100]]
    print("序号\t名称\t原价\t优惠价格")
    for i in range(0, len(shop)):
        print(f"{i + 1}\t{shop[i][0]}\t{shop[i][1]}\t{shop_con[i][1]}")
else:
    print("商品信息")
    shop_con=[["水", 1], ["辣条", 2], ["瓜子", 5], ["饼干", 8], ["肉", 10], ["电脑", 80]]
    print("序号\t名称\t原价\t优惠价格")
    for i in range(0, len(shop)):
        print(f"{i + 1}\t{shop[i][0]}\t{shop[i][1]}\t{shop_con[i][1]}")
# 加购
while True:
    a = input("请输入商品序号，加入购物车。输入Q/q结算")
    if a.isdigit() and int(a) <= len(shop):
        if money >= shop[int(a)-1][1]:
            money = money-shop[int(a)-1][1]
            car.append(shop[int(a)-1])
            print("成功加入购物车")
            print(f"当前余额为{money}")
            while True:
                x = input("是否继续？yes/no")
                if x == "yes":
                    break
                elif x == "no":
                    break
                else:
                    print("输入错误，请重新输入")
                    continue
            if x == "no":
                break
        else:
            y=input("余额不足，无法添加该商品，是否继续添加？yes/no")
            if y=='yes':
                continue
            else:
                break
    elif a == 'q' or a == 'Q':
        break
    else:
        print("商品不存在")

print("正在结账……")
# 打印购物小条
print("打印购物小票")
for index in car:
    print(index)
print("所剩余额为",money)